package com.elder.business.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.ApartmentRoomInfo;
import com.elder.business.mapper.ApartmentRoomInfoMapper;
import com.elder.common.core.domain.entity.SysDept;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.enums.WorkState;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.StringNewUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elder.business.mapper.MemberInfoMapper;
import com.elder.business.domain.MemberInfo;
import com.elder.business.service.IMemberInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 成员信息Service业务层处理
 * 
 * @author elder
 * @date 2023-11-26
 */
@Service
public class MemberInfoServiceImpl implements IMemberInfoService 
{
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private ApartmentRoomInfoMapper apartmentRoomInfoMapper;
    /**
     * 查询成员信息
     * 
     * @param memberId 成员信息主键
     * @return 成员信息
     */
    @Override
    public MemberInfo selectMemberInfoByMemberId(Long memberId)
    {
        return memberInfoMapper.selectMemberInfoByMemberId(memberId);
    }

    /**
     * 查询成员信息列表
     * 
     * @param memberInfo 成员信息
     * @return 成员信息
     */
    @Override
    public List<MemberInfo> selectMemberInfoList(MemberInfo memberInfo)
    {
        return memberInfoMapper.selectMemberInfoList(memberInfo);
    }

    /**
     * 新增成员信息
     * 
     * @param memberInfo 成员信息
     * @return 结果
     */
    @Override
    public int insertMemberInfo(MemberInfo memberInfo)
    {
        memberInfo.setCreateTime(DateUtils.getLongTime());
        return memberInfoMapper.insertMemberInfo(memberInfo);
    }

    /**
     * 修改成员信息
     * 
     * @param memberInfo 成员信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMemberInfo(MemberInfo memberInfo)
    {
        memberInfo.setUpdateTime(DateUtils.getLongTime());
        memberInfoMapper.updateMemberInfo(memberInfo);
        CompletableFuture.runAsync(()->{
            List<String> list = memberInfo.getRoom();
            Long memberId = memberInfo.getMemberId();
            List<Long> roomIds = list.stream().map(Long::parseLong).collect(Collectors.toList());
            apartmentRoomInfoMapper.updateRoomNurseSon(roomIds,memberId);
        });
        return 1;
    }

    /**
     * 批量删除成员信息
     * 
     * @param memberIds 需要删除的成员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberIds(Long[] memberIds)
    {
        return memberInfoMapper.deleteMemberInfoByMemberIds(memberIds);
    }

    /**
     * 删除成员信息信息
     * 
     * @param memberId 成员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberId(Long memberId)
    {
        return memberInfoMapper.deleteMemberInfoByMemberId(memberId);
    }

    @Override
    public TableDataInfo getMemberList(String memberName,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<MemberInfo> list = memberInfoMapper.getMemberList(memberName,104);
        PageInfo<MemberInfo> page = new PageInfo<>(list);
        List<MemberInfo> memberInfoList = page.getList();
        List<Long> memberIds = memberInfoList.stream().map(MemberInfo::getMemberId).collect(Collectors.toList());
        List<ApartmentRoomInfo> roomInfos = apartmentRoomInfoMapper.getRoomInfoByNurseId(memberIds);
        Map<Long, List<ApartmentRoomInfo>> roomMap = roomInfos.stream().collect(Collectors.groupingBy(ApartmentRoomInfo::getNurseId));
        List<JSONObject> memberList = memberInfoList.stream().map(v -> {
            JSONObject obj = new JSONObject();
            obj.put("memberId", StringNewUtils.long2Str(v.getMemberId()));
            obj.put("userId", StringNewUtils.long2Str(v.getUserId()));
            obj.put("memberName", v.getMemberName());
            obj.put("age", v.getAge());
            obj.put("sex", v.getSex());
            obj.put("room",
                    Objects.isNull(roomMap.get(v.getMemberId()))?"":(
                            String.join(",", roomMap.get(v.getMemberId()).stream().map(ApartmentRoomInfo::getRoomCode).collect(Collectors.toList())
                            )));
            obj.put("status", WorkState.getDisplayName(v.getStatus()));
            obj.put("brief", v.getBrief());
            return obj;
        }).collect(Collectors.toList());
        TableDataInfo info = new TableDataInfo();
        info.setTotal(page.getTotal());
        info.setCode(200);
        info.setMsg("success");
        info.setRows(memberList);
        return info;
    }

    @Override
    public JSONObject getAllMemberInfoList() {
        List<MemberInfo> memberList = memberInfoMapper.getMemberList(null,null);
        List<JSONObject> list = memberList.stream().map(v -> {
            JSONObject obj = new JSONObject();
            obj.put("memberId", StringNewUtils.long2Str(v.getMemberId()));
            obj.put("memberName", v.getMemberName());
            return obj;
        }).collect(Collectors.toList());
        JSONObject result = new JSONObject();
        result.put("code",200);
        result.put("data",list);
        return result;
    }

    @Override
    public JSONObject getNUrseMemberList() {
        List<MemberInfo> memberList = memberInfoMapper.getMemberListByDept(104);
//        List<Long> memberIds = memberList.stream().map(MemberInfo::getMemberId).collect(Collectors.toList());
//        List<ApartmentRoomInfo> roomInfos = apartmentRoomInfoMapper.getRoomInfoByNurseId(memberIds);
        List<JSONObject> list = memberList.stream().map(v -> {
            JSONObject obj = new JSONObject();
            obj.put("memberId",StringNewUtils.long2Str(v.getMemberId()));
            obj.put("memberName",v.getMemberName());
            return obj;
        }).collect(Collectors.toList());
        JSONObject result = new JSONObject();
        result.put("code",200);
        result.put("data",list);
        return result;
    }

    @Override
    public JSONObject getMemberOneselfInfo(Long userId) {
        MemberInfo member = memberInfoMapper.getMember(userId);
        JSONObject obj = new JSONObject();
        if(Objects.nonNull(member)){
            obj.put("memberId",StringNewUtils.long2Str(member.getMemberId()));
            obj.put("memberName",member.getMemberName());
            obj.put("userId",StringNewUtils.formatStr(member.getUserId()));
            obj.put("age",member.getAge());
            obj.put("sex",member.getSex());
            obj.put("edu",member.getEdu());
            obj.put("school",member.getSchool());
            obj.put("height",member.getHeight());
            obj.put("status",WorkState.getDisplayName(member.getStatus()));
            obj.put("brief",member.getBrief());
           return obj;
       }
        return obj;
    }

    @Override
    public Integer updateMember(MemberInfo memberInfo) {
        memberInfo.setUpdateTime(DateUtils.getLongTime());
        memberInfo.setStatus(WorkState.getValue(memberInfo.getDutyStatus()));
        return memberInfoMapper.updateMemberInfo(memberInfo);
    }
}
